import config

class anim_c:
    """
    Should add a timer var to the caller object, by passing a reference in from the parent of the parent then 
    just going parent.timer=blah.  Use this to keep the anim rate to whatever it needs to be.

    Animations might have a mask anim loaded from a seperate file
    """

    #offset is the x and y offset in the image of the set of frames we're after, handle X and Y are basically subtracted from the x and y of where you try and draw it to make it easier to get in the right spot
    def __init__(self, surface, gameObj, animName, frameList, offsetX=0, offsetY=0, handleX=0, handleY=0, sizeX=config.spriteSize[0], sizeY=config.spriteSize[1], loopAnim=True, animDelay=1):
        totalFrames=0

        self.surface = surface
        self.gameObj = gameObj
        self.toSurface = gameObj.screen
        self.redrawList = gameObj.redrawArea
        self.viewPortalSize = gameObj.viewPortalSize

        #set up the draw offset
        self.offsetX = sizeX * offsetX
        self.offsetY = sizeY * offsetY

        self.handleX = handleX
        self.handleY = handleY

        self.sizeX = sizeX
        self.sizeY = sizeY

        self.animName = animName

        self.frameList = frameList
        self.lastFrameNum = len(frameList)-1

        self.animDelay = animDelay
        self.loopAnim = loopAnim
        self.mask = None

        #print animName + ":" + str(frameList) + " -- Delay: " + str(animDelay)
    
    #lastFrame is the number of the last frame drawn, incFrame is whether to move to the next frame, undraw 
    def draw(self, x, y, lastFrame, incFrame, undraw, timer=0):
        
        #if the timer has ticked down and the frame is to be incremented, do it
        if timer==0 and incFrame:
            frame = lastFrame + 1
            if frame > self.lastFrameNum:
                if self.loopAnim == True:
                    frame = 0
                else:
                    frame = self.lastFrameNum

            timer = self.animDelay

        #else redraw the last frame and tick down
        else:
            frame=lastFrame
            timer-=1

        #determine if we are at the very end of the animation
        animEnded=False
        if frame==self.lastFrameNum and timer==0:
            animEnded=True

        pos = (x - self.handleX, y-self.handleY)
        size = [0,0]
        if self.viewPortalSize[0]>pos[0] and self.viewPortalSize[1]>pos[1]:
            if self.viewPortalSize[0]<self.sizeX:
                size[0]=self.viewPortalSize[0]
            else:
                size[0]=self.sizeX
            if self.viewPortalSize[1]<self.sizeY:
                size[1]=self.viewPortalSize[1]
            else:
                size[1]=self.sizeY

            #blit the frame which is number $frame in the list
            self.toSurface.blit(self.surface, (x - self.handleX, y - self.handleY), [self.frameList[frame] * self.sizeX, self.offsetY, size[0], size[1]])
            
            #draw the mask, this is a bit crappy
            if self.mask!= None:
                self.gameObj.crntMap.mapSurface.blit(self.mask.surface, (x - self.handleX, y - self.handleY), [self.frameList[frame] * self.sizeX, self.mask.offsetY, size[0], size[1]])
                #self.toSurface.blit(self.mask.surface, (50, 50), [self.frameList[frame] * self.sizeX, self.mask.offsetY, 64, 64])
            #else:
                #self.toSurface.blit(self.surface, (x - self.handleX, y - self.handleY), [self.frameList[frame] * self.sizeX, self.offsetY, size[0], size[1]])
            
            
    
        #this seems to work
        if undraw==True:
            self.redrawList += [(x - self.handleX, y - self.handleY, self.sizeX, self.sizeY)]

        #this class doesn't keep track of it's frame since 1 instance should be able to be called from all over the place, hence it's caller needs to keep track of the frame.  frame is whether this is the last frame or not
        return frame, animEnded, timer

    def setMask(self, anim):
        self.mask = anim

